const app = getApp()
Page({
    data: {
        photo_src: '/images/activities/加号.png',

        diary_name: '',
        comments: '',
        diary: {}
    },
    addPhoto() {
        swan.chooseImage({
            count: 1,
            success: res => {
                console.log(res);
                let _photo_src = res.tempFilePaths[0]
                this.setData({
                    photo_src: _photo_src
                })
            }
        });
    },
    public() {
        if (this.data.comments.length === 0 || this.data.diary_name.length === 0) {
            swan.showToast({
                title: '有字段未填写哟~',
                icon: "none",
            });
            return
        }
        if (this.data.photo_src.indexOf('加号') !== -1) {
            swan.showToast({
                title: '请上传游记图',
                icon: 'none'
            });
            return
        }
        const diary = {
            user: app.globalData.user.user_name,
            user_src: app.globalData.user.user_src,
            site: '',
            diary_name: this.data.diary_name,
            publish_date: Date.now(),
            is_public: true,
            photo_src: this.data.photo_src,
            comments: this.data.comments,
            favorites: 0,
            collections: 0,
        }

        const posturl = 'https://www.idear.cloud/idear/activity/save';
        app.myPost(posturl, diary, (err, res) => {
            if (err) {
                swan.showToast({
                    title: '游记发表失败 请重试',
                    icon: "none",
                    duration: 900
                });
            } else {
                swan.showToast({
                    title: '发表成功',
                    icon: "success",
                    duration: 900
                });
                setTimeout(() => {
                    swan.navigateBack({})
                }, 1000);
            }
        })

    },
    inputDiaryName(e) {
        const diary_name = e.detail.value
        this.setData({
            diary_name
        })

    },
    inputComments(e) {
        const comments = e.detail.value
        this.setData({
            comments
        })

    },
});